package com.zsj.juc.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author zsj
 * @version 1.0
 * @date 2024/4/19 17:52
 * @description
 */
public class CompletionStageTest05 {

    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);
        try {
            CompletableFuture<String> a = CompletableFuture.supplyAsync(() -> {
                System.out.println("a start");
                System.out.println("a :" + Thread.currentThread().getName());
                try {
                    TimeUnit.MILLISECONDS.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "playA";
            });

            CompletableFuture<String> b = CompletableFuture.supplyAsync(() -> {
                System.out.println("b start");
                System.out.println("b :" + Thread.currentThread().getName());
                try {
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "playB";
            });
            //比较哪个线程比较快
            System.out.println(a.applyToEither(b, f -> {
                return f + "赢了";
            }).get());


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }

    }
}
